<!--
 * @Author: JiraiyaChen 979526234@qq.com
 * @Date: 2025-03-07 13:51:35
 * @LastEditors: JiraiyaChen 979526234@qq.com
 * @LastEditTime: 2025-05-26 13:23:48
 * @Description: 新增 评审项 (表头) 
-->

<template>
  <ClDialog v-bind="$attrs" :title="getTitle" width="1390px">
    <ClBar :loading="loadedFrom" :bar-height="isView ? '700px' : '400px'">
      <ClForm
        ref="formRef"
        label-width="150px"
        :form-item="formItem"
        :disabled="disabled"
        @on-change-select="onSelectChange"
      />
      <div v-if="isView">
        <Collapse :collapse-item-list="pointInfoData" :info="props.info" />
      </div>
    </ClBar>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" :disabled="disabled" @click="onSure">确定</el-button>
      </span>
    </template>
  </ClDialog>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { ElMessage } from 'element-plus'
import Collapse from './components/Collapse.vue'
import { ADD_FORM_ITEM, INCLUDE_CODE, PROJECT_ADD_FORM_ITEM } from './data'
import { getReviewItemView, pointInfoView } from '@/api/basic-settings-module'
import { projectItemInfoView, projectPointInfoView } from '@/api/project-scoring-standard-configuration'
import { useEnumeration } from '@/store/useEnumeration'
onMounted(() => {
  initForm()
})
const pointInfoData = ref([])
const formItem = ref<any[]>([])
const formItemTemp = cloneDeep(ADD_FORM_ITEM)
const formItemProject = cloneDeep(PROJECT_ADD_FORM_ITEM)
const formIncludeItem = ref(cloneDeep(INCLUDE_CODE))

const { getEnumValue, getMarkModeEnum, getScoreCollectWayEnum, getYesOrNoEnum, getFlowOrderEnum } = useEnumeration()
const loadedFrom = ref(false)
const initForm = async () => {
  try {
    loadedFrom.value = false
    const [markModeEnum, scoreCollectWayEnum, yesOrNoEnum, flowOrderEnum]: any = await Promise.all([
      getMarkModeEnum(),
      getScoreCollectWayEnum(),
      getYesOrNoEnum(), // 是否暗标 ,是否纳入总分
      getFlowOrderEnum()
    ])
    // 设置表单 下拉
    formIncludeItem.value.elementConfig.options = yesOrNoEnum
    if (isProject.value) {
      formItem.value = formItemProject
    } else {
      formItem.value = formItemTemp
    }
    formItem.value.forEach((item) => {
      if (item.key === 'markModeCode') {
        item.elementConfig.options = markModeEnum
        item.elementConfig.disabled = props.info.id ? true : false
      }
      if (item.key === 'scoreCollectWayCode') {
        item.elementConfig.options = scoreCollectWayEnum
      }
      if (item.key === 'darkMarkCode') {
        item.elementConfig.options = yesOrNoEnum
      }
      if (item.key === 'includeCode') {
        item.elementConfig.options = yesOrNoEnum
      }
      if (item.key === 'flowOrderCode') {
        item.elementConfig.options = flowOrderEnum
      }
    })

    // 设置表单 默认值
    if (props.type === '编辑评审项') {
      const res: any = await getReviewItemView({ id: props.info.id })
      setPropsFromData(res)
    }
    if (props.type === '查看评审项') {
      const res: any = await getReviewItemView({ id: props.info.id })
      const pointInfoRes: any = await pointInfoView({ itemId: props.info.id })
      pointInfoData.value = pointInfoRes
      setPropsFromData(res)
    }
    if (props.type === '查看项目评审项') {
      const res: any = await projectItemInfoView({ id: props.info.id })
      const pointInfoRes: any = await projectPointInfoView({ itemId: props.info.id })
      pointInfoData.value = pointInfoRes
      setPropsFromData(res)
    }
    if (props.type === '编辑项目评审项') {
      setPropsFromData(props.info)
    }

    loadedFrom.value = true
  } catch (error) {
    loadedFrom.value = true
    console.log(error)
  }
}

// 打分方式 是 符合性评审 的时候 评分点汇总方式默认为一票否决
const tempSoreCollectWayCode = ref('')
// 设置表单数据
const setPropsFromData = (res: any) => {
  formIncludeItem.value.value = res.includeCode
  tempSoreCollectWayCode.value = res.scoreCollectWayCode
  formItem.value.forEach((item) => (item.value = res[item.key]))
  changeFormItem(res.markModeCode)
}

const formRef = ref()
// 获取评审项 表单数据
const getFormData = (res: any) => {
  const markModeName = getEnumValue(res.markModeCode, 'markModeEnum')
  const scoreCollectWayName = getEnumValue(res.scoreCollectWayCode, 'scoreCollectWayEnum')
  const darkMarkName = getEnumValue(res.darkMarkCode, 'yesOrNoEnum')
  const includeName = getEnumValue(res.includeCode, 'yesOrNoEnum')
  const flowOrderName = getEnumValue(res.flowOrderCode, 'flowOrderEnum')
  const apiParams = {
    id: props.info.id,
    name: res.name,
    markModeCode: res.markModeCode,
    markModeName,
    scoreCollectWayCode: res.scoreCollectWayCode,
    scoreCollectWayName,
    darkMarkCode: res.darkMarkCode,
    darkMarkName,
    includeCode: res.includeCode,
    includeName,
    description: res.description,
    flowOrderCode: res.flowOrderCode,
    flowOrderName,
    orderNo: res.orderNo
  }
  return apiParams
}

// 关闭弹窗
const closeDialog = () => {
  emit('atClose')
}
// 验证表单
const onSure = async () => {
  try {
    // 先验证表单
    const res = await formRef.value.refElForm.validate()
    if (res) {
      // 验证通过
      // 获取评审项表单数据
      const res1 = getFormData(formRef.value.formData)
      // 获取评分点表单数据
      await props.saveApi({ ...res1 })
      emit('atClose', 'refresh-table')
    } else {
      ElMessage.warning('请检查表单')
    }
  } catch (error) {
    console.log(error)
  }
}

// 下拉框改变
const onSelectChange = async (item: any) => {
  const { key, value } = item
  if (key === 'markModeCode') {
    changeFormItem(value)
  }
}
// 改变表单
const changeFormItem = (value: any) => {
  // 若评审项中的打分方式选择符合性打分，则评分点汇总方式默认为一票否决
  const scoreCollectWayIns = formItem.value.find((item) => item.key === 'scoreCollectWayCode')
  scoreCollectWayIns.value = value === 'CONFORM' ? 'ONE_VOTE_AGAINST' : tempSoreCollectWayCode.value
  // 打分方式 是 符合性评审 的时候  和 是否纳入总分
  if (value === 'CONFORM') {
    formItem.value = formItem.value.filter((item) => !['includeCode'].includes(item.key))
  }
  // 打分方式 是 直接打分 的时候 显示  是否纳入总分
  if (value === 'DIRECT') {
    const includeCodeIns = formItem.value.findIndex((item) => item.key === 'includeCode')
    if (includeCodeIns === -1) {
      formItem.value.splice(4, 0, formIncludeItem.value)
    }
  }
}

const getTitle = computed(() => {
  return props.type
})

interface infoType {
  [props: string]: any
}
const props = defineProps<{
  info: infoType
  type: '新增评审项' | '编辑评审项' | '查看评审项' | '新增项目评审项' | '编辑项目评审项' | '查看项目评审项' | string
  saveApi: Function // 保存接口
}>()
const disabled = computed(() => {
  return props.info.disabled || false
})
const isView = computed(() => {
  // 查看评审项 和 查看项目评审项 的时候 显示 评分点
  return props.type === '查看评审项' || props.type === '查看项目评审项'
})
const isProject = computed(() => {
  return props.type === '新增项目评审项' || props.type === '编辑项目评审项' || props.type === '查看项目评审项'
})
const emit = defineEmits<{
  (e: 'atClose', str?: string | undefined): void
}>()
</script>

<style scoped lang="scss">
.rating-points-btn-container {
  display: flex;
  justify-content: center;
  margin: 15px 0;
}

.rating-points-btn {
  width: 400px;
  border-style: dotted;
}
</style>
